package com.justech.mes.utils;

import com.google.common.collect.Lists;
import com.justech.mes.model.MesAbnormalRecordVO;
import com.justech.mes.model.MesEquipment;
import com.justech.mes.model.MesEquipmentModule;
import com.justech.mes.model.MesModule;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

/**
 * 字符串转化工具类
 *
 * @author jacky
 */
public class StrUtil {

    public static String getUUID32() {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        return uuid;
    }

    /**
     * 字符串转为驼峰
     *
     * @param str
     * @return
     */
    public static String str2hump(String str) {
        StringBuffer buffer = new StringBuffer();
        if (str != null && str.length() > 0) {
            String underline = "_";
            if (str.contains(underline)) {
                String[] chars = str.split(underline);
                int size = chars.length;
                if (size > 0) {
                    List<String> list = Lists.newArrayList();
                    for (String s : chars) {
                        if (s != null && s.trim().length() > 0) {
                            list.add(s);
                        }
                    }

                    size = list.size();
                    if (size > 0) {
                        buffer.append(list.get(0));
                        for (int i = 1; i < size; i++) {
                            String s = list.get(i);
                            buffer.append(s.substring(0, 1).toUpperCase());
                            if (s.length() > 1) {
                                buffer.append(s.substring(1));
                            }
                        }
                    }
                }
            } else {
                buffer.append(str);
            }
        }

        return buffer.toString();
    }


    public static String getURLContent(String urlStr) {
        //请求的url
        URL url = null;
        //请求的输入流
        BufferedReader in = null;
        //输入流的缓冲
        StringBuffer sb = new StringBuffer();
        try {
            url = new URL(urlStr);
            in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            String str = null;
            //一行一行进行读入
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
        } catch (Exception ex) {

        } finally {
            try {
                if (in != null) {
                    in.close(); //关闭流
                }
            } catch (IOException ex) {

            }
        }
        String result = sb.toString();
        return result;
    }

    /**
     * 字符串转换为对应日期
     *
     * @param source
     * @param pattern
     * @return
     */
    public static Date stringToDate(String source, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse(source);
        } catch (Exception e) {
        }
        return date;
    }


    public static void sameAndDifferentModule(List<MesModule> erpdb, List<MesModule> mesdb, List<MesModule> sameItems,
                                              List<MesModule> addItems, List<MesModule> delBItems) {

        if (erpdb.isEmpty() && mesdb.isEmpty()) {
            return;
        }

        LinkedHashMap<String, MesModule> map = new LinkedHashMap<>();
        LinkedHashMap<String, MesModule> sameMap = new LinkedHashMap<>();
        LinkedHashMap<String, MesModule> onlyBMap = new LinkedHashMap<>();
        for (MesModule item : erpdb) {
            String key = item.getWorkNo() + "_" + item.getModNo() + "_" + DateUtil.dateToStrLong(item.getCreateTime());
            map.put(key, item);
        }

        for (MesModule item : mesdb) {
            String key = item.getWorkNo() + "_" + item.getModNo() + "_" + DateUtil.dateToStrLong(item.getCreateTime());
            if (map.containsKey(key)) {

                //如果要更新a中的记录，则要更新map中的元素，并添加到sameMap
                MesModule mesModule = map.get(key);
                sameMap.put(key, mesModule);
            } else {
                //新建的元素，需要设置相关成员变量
                onlyBMap.put(key, item);
            }
        }

        sameItems.addAll(sameMap.values());
        delBItems.addAll(onlyBMap.values());

        for (MesModule item : sameItems) {
            String key = item.getWorkNo() + "_" + item.getModNo() + "_" + DateUtil.dateToStrLong(item.getCreateTime());
            map.remove(key);
        }
        addItems.addAll(map.values());

    }

    public static void sameAndDifferentEquipment(List<MesEquipment> erpdb, List<MesEquipment> mesdb, List<MesEquipment> sameItems,
                                                 List<MesEquipment> addItems, List<MesEquipment> delBItems) {

        if (erpdb.isEmpty() && mesdb.isEmpty()) {
            return;
        }

        LinkedHashMap<String, MesEquipment> map = new LinkedHashMap<>();
        LinkedHashMap<String, MesEquipment> sameMap = new LinkedHashMap<>();
        LinkedHashMap<String, MesEquipment> onlyBMap = new LinkedHashMap<>();
        for (MesEquipment item : erpdb) {
            String key = item.getEquipmentCode() + "_" + item.getNameplateNo();
            map.put(key, item);
        }

        for (MesEquipment item : mesdb) {
            String key = item.getEquipmentCode() + "_" + item.getNameplateNo();
            if (map.containsKey(key)) {

                //如果要更新a中的记录，则要更新map中的元素，并添加到sameMap
                MesEquipment mesModule = map.get(key);
                sameMap.put(key, mesModule);
            } else {
                //新建的元素，需要设置相关成员变量
                onlyBMap.put(key, item);
            }
        }

        sameItems.addAll(sameMap.values());
        delBItems.addAll(onlyBMap.values());

        for (MesEquipment item : sameItems) {
            String key = item.getEquipmentCode() + "_" + item.getNameplateNo();
            map.remove(key);
        }
        addItems.addAll(map.values());

    }

    public static void sameAndDifferentMesEquipmentModule(List<MesEquipmentModule> erpdb, List<MesEquipmentModule> mesdb, List<MesEquipmentModule> sameItems,
                                                          List<MesEquipmentModule> addItems, List<MesEquipmentModule> delBItems) {

        if (erpdb.isEmpty() && mesdb.isEmpty()) {
            return;
        }

        LinkedHashMap<String, MesEquipmentModule> map = new LinkedHashMap<>();
        LinkedHashMap<String, MesEquipmentModule> sameMap = new LinkedHashMap<>();
        LinkedHashMap<String, MesEquipmentModule> onlyBMap = new LinkedHashMap<>();
        for (MesEquipmentModule item : erpdb) {
            String key = item.getNameplateNo() + "_" + item.getModNo() + "_" + item.getWorkNo() + "_" + DateUtil.dateToStrLong(item.getCreateTime());
            map.put(key, item);
        }

        for (MesEquipmentModule item : mesdb) {
            String key = item.getNameplateNo() + "_" + item.getModNo() + "_" + item.getWorkNo() + "_" + DateUtil.dateToStrLong(item.getCreateTime());
            if (map.containsKey(key)) {

                //如果要更新a中的记录，则要更新map中的元素，并添加到sameMap
                MesEquipmentModule mesModule = map.get(key);
                sameMap.put(key, mesModule);
            } else {
                //新建的元素，需要设置相关成员变量
                onlyBMap.put(key, item);
            }
        }

        sameItems.addAll(sameMap.values());
        delBItems.addAll(onlyBMap.values());

        for (MesEquipmentModule item : sameItems) {
            String key = item.getNameplateNo() + "_" + item.getModNo() + "_" + item.getWorkNo() + "_" + DateUtil.dateToStrLong(item.getCreateTime());
            map.remove(key);
        }
        addItems.addAll(map.values());

    }

    public static void sameAndDifferentRecordtime(List<MesAbnormalRecordVO> erpdb, List<MesAbnormalRecordVO> mesdb, List<MesAbnormalRecordVO> sameItems,
                                                  List<MesAbnormalRecordVO> addItems, List<MesAbnormalRecordVO> delBItems) {

        if (erpdb.isEmpty() && mesdb.isEmpty()) {
            return;
        }

        LinkedHashMap<String, MesAbnormalRecordVO> map = new LinkedHashMap<>();
        LinkedHashMap<String, MesAbnormalRecordVO> sameMap = new LinkedHashMap<>();
        LinkedHashMap<String, MesAbnormalRecordVO> onlyBMap = new LinkedHashMap<>();
        for (MesAbnormalRecordVO item : erpdb) {
            String key = item.getAbnormalUuid() + "_" + item.getUsername();
            map.put(key, item);
        }

        for (MesAbnormalRecordVO item : mesdb) {
            String key = item.getAbnormalUuid() + "_" + item.getUsername();
            if (map.containsKey(key)) {

                //如果要更新a中的记录，则要更新map中的元素，并添加到sameMap
                MesAbnormalRecordVO recordtime = map.get(key);
                sameMap.put(key, recordtime);
            } else {
                //新建的元素，需要设置相关成员变量
                onlyBMap.put(key, item);
            }
        }

        sameItems.addAll(sameMap.values());
        delBItems.addAll(onlyBMap.values());

        for (MesAbnormalRecordVO item : sameItems) {
            String key = item.getAbnormalUuid() + "_" + item.getUsername();
            map.remove(key);
        }
        addItems.addAll(map.values());

    }

    /**
     * 不够位数的在前面补0，保留num的长度位数字
     *
     * @param code
     * @return
     */
    public static String autoGenericCode(String code, int num) {
        String result = "";
        // 保留num的位数
        // 0 代表前面补充0
        // num 代表长度为4
        // d 代表参数为正数型
        result = String.format("%0" + num + "d", Integer.parseInt(code));

        return result;
    }
}
